package com.lcz.audioplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.lcz.audioplayer.bean.AudioBean;
import com.lcz.audioplayer.databinding.ActivityPlayBinding;
import com.lcz.audioplayer.util.NotificationsUtils;
import com.lcz.audioplayer.util.VolumeUtil;

import java.util.ArrayList;


public class PlayActivity extends AppCompatActivity {
    String TAG = PlayActivity.class.getSimpleName();
    private IPlayerClient iPlayerClient;
    ;
    ActivityPlayBinding binding;
    ArrayList<AudioBean> voiceList;
    private PlayerManagerCallback listener = new PlayerManagerCallback() {
        @Override
        public void onProgressChange(int currentProgress, int total, String currentPositionStr, String totalDurationStr) {
            binding.seekBar.setMax(total);
            binding.seekBar.setProgress(currentProgress);
            binding.tvPosition.setText(currentPositionStr);
            binding.tvTotal.setText(totalDurationStr);
        }

        @Override
        public void onPlayStart() {
            //开始播放，修改UI成暂停的按钮
            binding.tvTitle.setText(iPlayerClient.getAudioList().get(iPlayerClient.getCurrentIndex()).getName());
            Glide.with(PlayActivity.this)
                    .load(iPlayerClient.getAudioList().get(iPlayerClient.getCurrentIndex()).getImage())
                    .into(binding.ivCover);
            binding.playOrPauseBtn.setImageResource(R.drawable.selector_palyer_pause);
        }

        @Override
        public void onPlayPause() {
            binding.playOrPauseBtn.setImageResource(R.drawable.selector_palyer_play);
        }

        @Override
        public void onPlayStop() {
            binding.playOrPauseBtn.setImageResource(R.drawable.selector_palyer_play);
        }

        @Override
        public void onPlayError(MediaPlayer mp, int what, int extra) {

        }

        @Override
        public void onCompletion() {
            iPlayerClient.playNext();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPlayBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!NotificationsUtils.isNotificationEnabled(this)) {
            NotificationsUtils.openPush(this);
        }
        iPlayerClient = PlayerClient.getInstance();

        iPlayerClient.registerListener(listener);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//保持屏幕常量
        voiceList = creatAudioList();
        iPlayerClient.setVoiceList(voiceList);
        iPlayerClient.playByIndex(0);
        binding.playOrPauseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果现在的状态是正在播放的,那么就暂停
                if (iPlayerClient.isPlaying()) {
                    iPlayerClient.pause();
                } else {
                    //如果现在的状态是非播放的,那么我们就让播放器播放节目
                    iPlayerClient.play();
                }
            }
        });

        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean isFromUser) {
                if (isFromUser) {
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //手离开拖动进度条的时候更新进度
                iPlayerClient.seekTo(seekBar.getProgress());
            }
        });
        binding.playPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iPlayerClient.playPre();

            }
        });

        binding.playNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //播放下一个节目
                iPlayerClient.playNext();
            }
        });


        VolumeUtil volumeUtil = new VolumeUtil(this);
        int mediaVolume = volumeUtil.getMediaVolume();
        int mediaMaxVolume = volumeUtil.getMediaMaxVolume();
        Log.i(TAG, "" + mediaVolume);
        Log.i(TAG, "" + mediaMaxVolume);
        binding.tvMinusVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int mediaVolume = volumeUtil.getMediaVolume();
                if (mediaVolume == 0) {

                } else {
                    mediaVolume--;

                }
                volumeUtil.setMediaVolume(mediaVolume);
            }
        });
        binding.tvAddVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int mediaVolume = volumeUtil.getMediaVolume();
                int mediaMaxVolume = volumeUtil.getMediaMaxVolume();
                if (mediaVolume == mediaMaxVolume) {

                } else {
                    mediaVolume++;

                }
                volumeUtil.setMediaVolume(mediaVolume);
            }
        });


    }

    private ArrayList<AudioBean> creatAudioList() {
        ArrayList<AudioBean> voiceList = new ArrayList<>();
        addOneBean(voiceList,"001序言",
                "http://aod.cos.tx.xmcdn.com/storages/929c-audiofreehighqps/10/69/CKwRIaIFIJcHADLmYADldNGx.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"002导论 中国近代史开场白 上",
                "http://aod.cos.tx.xmcdn.com/storages/e452-audiofreehighqps/B6/E5/CKwRIW4FIJchAD6kFwDldNgz.mp3",
                "http://imgopen.xmcdn.com/storages/f9f3-audiofreehighqps/AA/87/CMCoOSED6WSFAARZLACBMWHf.jpg!op_type=3&columns=100&rows=100");
        addOneBean(voiceList,"003导论 中国近代史开场白 下",
                "http://aod.cos.tx.xmcdn.com/storages/1a83-audiofreehighqps/88/B0/CMCoOSEDjkiBAFuQMQBpHJxM.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"004第一讲 第一节 中西近代史的不同开端",
                "http://aod.cos.tx.xmcdn.com/storages/981d-audiofreehighqps/86/CC/CMCoOSMDjnzxAEhMwwBpKplA.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"005第一讲 第二节 中国和英国的第一次直接碰撞 上",
                "http://aod.cos.tx.xmcdn.com/storages/a9a0-audiofreehighqps/7F/64/CMCoOSADkE4QAG9IWwBpp8Wl.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"006第一讲 第二节 中国和英国的第一次直接碰撞 下",
                "http://aod.cos.tx.xmcdn.com/storages/c204-audiofreehighqps/2F/99/CMCoOSYDkvEYAGQSJgBqbUy5.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"007第一讲 第三节 鸦片—打破中英贸易的不平衡",
                "http://aod.cos.tx.xmcdn.com/storages/49af-audiofreehighqps/B9/F0/CMCoOSEDlZQfAIJV6ABrNuJA.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"008第一讲 第四节 英国对中国政策的成本核算 中英的战争技术和战略对比",
                "http://aod.cos.tx.xmcdn.com/storages/e3f6-audiofreehighqps/2E/DA/CMCoOSYDmYieAIcyzgBsNRbS.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");
        addOneBean(voiceList,"009第一讲 第五节 开放教禁带来了西方的输入及传统历史缔造的两个神话",
                "http://aod.cos.tx.xmcdn.com/storages/d750-audiofreehighqps/1D/72/CKwRINsE0CZCAF4vMQDMcxoh.mp3",
                "http://imgopen.xmcdn.com/storages/a79e-audiofreehighqps/22/1D/CMCoOSMDjVbEAAwhZgBo3pld.jpg!op_type=3&columns=180&rows=180");

        return voiceList;
    }
    private void addOneBean(ArrayList<AudioBean> voiceList, String name, String playUrl, String imageUrl){
        AudioBean audioBean = new AudioBean();
        audioBean.setName(name);
        audioBean.setPlay_url(playUrl);
        audioBean.setImage(imageUrl);
        voiceList.add(audioBean);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        iPlayerClient.unRegisterListener(listener);
    }


}